#include "MainMenuState.h"
#include "GameState.h"
#include "CharSelect.h"
#include "OptionsState.h"
#include "CreditsState.h"
#include "GameMode.h"
#include "../Game.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "../SGD Wrappers/SGD_AudioManager.h"
#include "../SGD Wrappers/SGD_InputManager.h"
#include "../SGD Wrappers/SGD_String.h"
#include "../Font.h"
#include "../SGD Wrappers/SGD_Geometry.h"
#include <fstream>
using namespace std;


CMainMenuState* CMainMenuState::GetInstance(void)
{
	static CMainMenuState s_Instance;
	return &s_Instance;
}

void CMainMenuState::Enter()
{
	SGD::GraphicsManager::GetInstance()->SetClearColor({ 0, 0, 0 });
	
	SGD::AudioManager* pAudio = SGD::AudioManager::GetInstance();

	ball = SGD::GraphicsManager::GetInstance()->LoadTexture("pictures/ball.png");
	logo = SGD::GraphicsManager::GetInstance()->LoadTexture("pictures/logo_NBL_justpaint.png");
	button = SGD::GraphicsManager::GetInstance()->LoadTexture("pictures/1407_IForgot_blankbutton.png");
	background = SGD::GraphicsManager::GetInstance()->LoadTexture("pictures/1407_IForgot_menuscreen.png");

	bool fullscreen = false;
	int sounds = 100;
	int  music = 100;

	std::string filePath = Game::GetInstance()->GetSavePath();
	filePath += "options.txt";

	ifstream file(filePath, ios_base::in);
	if (file.is_open())
	{
		while (true)
		{
			if (file.eof())
				break;
			file >> fullscreen >> sounds >> music;
			file.ignore(INT_MAX, '\n');
		}
	}
	Game::GetInstance()->SetFullscreen(fullscreen);
	pAudio->SetAudioVolume(Game::GetInstance()->GetMenuMusic(), music);
	pAudio->SetAudioVolume(Game::GetInstance()->GetGameMusic(), music);
	pAudio->SetAudioVolume(Game::GetInstance()->Getcreatureatk(), sounds);
	pAudio->SetAudioVolume(Game::GetInstance()->Getcreaturedie(), sounds);
	pAudio->SetAudioVolume(Game::GetInstance()->Getcreaturegrowl(), sounds);
	pAudio->SetAudioVolume(Game::GetInstance()->Gethit(), sounds);

	file.close();
	
	if (Game::GetInstance()->GetFullscreen() == true)
		SGD::GraphicsManager::GetInstance()->Resize(SGD::Size{ Game::GetInstance()->GetScreenWidth(), Game::GetInstance()->GetScreenHeight() }, false);
}

void CMainMenuState::Exit()
{
	SGD::GraphicsManager::GetInstance()->UnloadTexture(ball);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(logo);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(button);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(background);
}

bool CMainMenuState::Input()
{
	SGD::InputManager* pInput = SGD::InputManager::GetInstance();
	SGD::AudioManager* pAudio = SGD::AudioManager::GetInstance();

	if (pInput->IsKeyPressed(SGD::Key::Escape) == true)	
		return false;
	
	// Move cursor
	
	if (pInput->IsKeyPressed(SGD::Key::Down) == true ||
		pInput->IsDPadPressed(0, SGD::DPad::Down) == true)
	{
		++m_nCursor;

		// Wrap around
		if (m_nCursor > 3)
			m_nCursor = 0;

		pAudio->PlayAudio(Game::GetInstance()->GetMenusound(), false);

	}	
	else if (pInput->IsKeyPressed(SGD::Key::Up) == true ||
		pInput->IsDPadPressed(0, SGD::DPad::Up) == true
		)
	{
		--m_nCursor;

		// Wrap around
		if (m_nCursor < 0)
			m_nCursor = 3;
		pAudio->PlayAudio(Game::GetInstance()->GetMenusound(), false);

	}

	if (pInput->IsKeyPressed(SGD::Key::Enter) == true || pInput->IsButtonPressed(0, 0))
	{
		if (m_nCursor == 0)
		{
			Game::GetInstance()->ChangeState(CGameMode::GetInstance());
			return true;
		}
		else if (m_nCursor == 1)
		{			
			Game::GetInstance()->ChangeState(COptionsState::GetInstance());
			return true;
		}
		else if (m_nCursor == 2)
		{
			Game::GetInstance()->ChangeState(CCreditsState::GetInstance()); 
			return true;
		}
		else if (m_nCursor == 3)
		{			
			return false;
			//exit
		}
	}
	return true;
}

void CMainMenuState::Update(float elapsedTime)
{
	SGD::InputManager* pInput = SGD::InputManager::GetInstance();
	SGD::AudioManager* pAudio = SGD::AudioManager::GetInstance();

	SGD::Vector c = pInput->GetLeftJoystick(0);

	Time += elapsedTime;	
	if (c.y > 0 && Time >= 0.2f)
	{
		++m_nCursor;

		// Wrap around
		if (m_nCursor > 3)
			m_nCursor = 0;
		pAudio->PlayAudio(Game::GetInstance()->GetMenusound(), false);

		Time = 0.0f;
	}

	else if (c.y < 0 && Time >= 0.2f)
	{
		--m_nCursor;

		// Wrap around
		if (m_nCursor < 0)
			m_nCursor = 3;
		pAudio->PlayAudio(Game::GetInstance()->GetMenusound(), false);

		Time = 0.0f;
	}


}

void CMainMenuState::Render()
{
	Game* pGame = Game::GetInstance();

	float width = pGame->GetScreenWidth();
	const Font* pFont = pGame->GetFont();
	
	SGD::GraphicsManager::GetInstance()->DrawTexture(background, { 0, 0 }, 0.0f, { 0, 0 }, {255,255,255});

	float darkOffset = pGame->DarkOff();
	float medOffset = pGame->MedOff();
	float lightOffset = pGame->LightOff();

	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetDarkSmoke(), { darkOffset, 0 }, 0.0f, { 0, 0 }, { 110, 255, 255, 255 });
	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetMedSmoke(), { medOffset, 0 }, 0.0f, { 0, 0 }, { 127, 255, 255, 255 });
	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetLightSmoke(), { lightOffset, 0 }, 0.0f, { 0, 0 }, { 135, 255, 255, 255 });

	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetDarkSmoke(), { darkOffset - 805, 0 }, 0.0f, { 0, 0 }, { 110, 255, 255, 255 });
	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetMedSmoke(), { medOffset - 805, 0 }, 0.0f, { 0, 0 }, { 127, 255, 255, 255 });
	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetLightSmoke(), { lightOffset - 805, 0 }, 0.0f, { 0, 0 }, { 135, 255, 255, 255 });

	SGD::GraphicsManager::GetInstance()->DrawTexture(logo, { 150, -50 }, 0.0f, {}, { 255, 255, 255 }, {1,0.5f});
	
	SGD::GraphicsManager::GetInstance()->DrawTexture(button, { 390, 140}, 0.5f, {}, { 255, 255, 255 }, { 0.7f, 0.7f });
	pFont->Draw("Play", { (width - (4 * 32)) / 2, 220 },
		0.7f, { 0, 255, 0 });
	
	SGD::GraphicsManager::GetInstance()->DrawTexture(button, { 310, 410 }, -1.7f, {}, { 255, 255, 255 }, { 0.7f, 0.7f });
	pFont->Draw("Options", { (width - (4 * 32)) / 2.3f, 330 },
		0.7f, { 0, 0, 255 });

	SGD::GraphicsManager::GetInstance()->DrawTexture(button, { 400, 350 }, 0.5f, {}, { 255, 255, 255 }, { 0.7f, 0.7f });
	pFont->Draw("Credits", { (width - (4 * 32)) / 2.3f, 440 },
		0.7f, { 255, 255, 255 });

	SGD::GraphicsManager::GetInstance()->DrawTexture(button, { 320, 620 }, -1.7f, {}, { 255, 255, 255 }, { 0.7f, 0.7f });
	pFont->Draw("Exit", { (width - (4 * 32)) / 2, 545 },
		0.7f, { 255, 0, 0 });

	SGD::GraphicsManager::GetInstance()->DrawTexture(ball, { (width - (4 * 32)) / 4.5f, 210.0f + 110 * m_nCursor }, 0.0f, {}, {}, {0.25f,0.25f});	
}